package com.authManager.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.annotation.Validated;
import org.apache.commons.lang3.StringUtils;

import com.authManager.entity.SysMenu;
import com.authManager.server.service.ISysMenuService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 【菜单信息】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-17
 */

@Api(value = "/admin/sysMenu", tags = "【菜单信息】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysMenu", produces = BaseController.defProduces)
public class SysMenuController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysMenuController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysMenuService sysMenuService;

	@ApiOperation(value = "添加【菜单信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "资源名称", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "menu_type等于0   的唯一 code    ", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrl", value = "资源路径", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrlType", value = "资源路径 跳转方式", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "parentId", value = "父级资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "site", value = "菜单所在位置（left  rigit top）", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "sortNum", value = "排序", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "icon", value = "资源图标", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "menuType", value = "资源类别 0系统 1菜单 2按钮 ", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "资源介绍", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "buttonMark", value = "按钮标签【查询query  列表查询queryList  增加save 编辑edit 删除 del】", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field1", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field2", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field3", value = "", required = false), })
	@RequestMapping(value = "saveSysMenu", method = { RequestMethod.POST })
	public ResultData<Object> saveSysMenu(
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 100, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = false) String systemCode,
			@RequestParam(required = false) @Length(min = 0, max = 255, message = "参数[pageUrl]最大长度不能超过{max}") String pageUrl,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[pageUrlType]最大长度不能超过{max}") String pageUrlType,
			@RequestParam(required = false) Long parentId,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[site]最大长度不能超过{max}") String site,
			@RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[icon]最大长度不能超过{max}") String icon,
			@RequestParam(required = true) Integer menuType, @RequestParam(required = false) String description,
			@RequestParam(required = false) String buttonMark, @RequestParam(required = false) String field1,
			@RequestParam(required = false) String field2, @RequestParam(required = false) String field3) {
		SysMenu sysMenu = new SysMenu();
		if (StringUtils.isNotBlank(name)) {
			sysMenu.setName(name);
		}
		if (StringUtils.isNotBlank(systemCode)) {
			SysMenu sysMenuTmp = sysMenuService
					.selectOne(new EntityWrapper<SysMenu>(new SysMenu().setSystemCode(systemCode)));
			if (sysMenuTmp != null) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【systemCode】的值：【" + systemCode + "】已经存在").build();
			}
			sysMenu.setSystemCode(systemCode);
		}
		if (StringUtils.isNotBlank(pageUrl)) {
			sysMenu.setPageUrl(pageUrl);
		}
		if (StringUtils.isNotBlank(pageUrlType)) {
			sysMenu.setPageUrlType(pageUrlType);
		}
		if (parentId != null) {
			sysMenu.setParentId(parentId);
		}
		if (StringUtils.isNotBlank(site)) {
			sysMenu.setSite(site);
		}
		if (sortNum != null) {
			sysMenu.setSortNum(sortNum);
		}
		if (StringUtils.isNotBlank(icon)) {
			sysMenu.setIcon(icon);
		}
		if (menuType != null) {
			sysMenu.setMenuType(menuType);
		}
		if (StringUtils.isNotBlank(description)) {
			sysMenu.setDescription(description);
		}
		if (StringUtils.isNotBlank(buttonMark)) {
			sysMenu.setButtonMark(buttonMark);
		}
		if (StringUtils.isNotBlank(field1)) {
			sysMenu.setField1(field1);
		}
		if (StringUtils.isNotBlank(field2)) {
			sysMenu.setField2(field2);
		}
		if (StringUtils.isNotBlank(field3)) {
			sysMenu.setField3(field3);
		}
		sysMenu.setCreater(getCurrUserLoginId());
		sysMenu.setCreateTime(new Date());
		sysMenu.setUpdater(null);
		sysMenu.setUpdateTime(null);
		sysMenuService.insert(sysMenu);
		Object sysMenuId = sysMenu.getSysMenuId();
		return new ResultData.Builder<Object>(ResultCode.success).setData(sysMenuId).build();
	}

	@ApiOperation(value = "修改【菜单信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuIdStr", value = "主键", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "资源名称", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "menu_type等于0   的唯一 code    ", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrl", value = "资源路径", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrlType", value = "资源路径 跳转方式", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "parentId", value = "父级资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "site", value = "菜单所在位置（left  rigit top）", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "sortNum", value = "排序", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "icon", value = "资源图标", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "menuType", value = "资源类别 0系统 1菜单 2按钮 ", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "资源介绍", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "buttonMark", value = "按钮标签【查询query  列表查询queryList  增加save 编辑edit 删除 del】", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field1", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field2", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field3", value = "", required = false), })
	@RequestMapping(value = "editSysMenu", method = { RequestMethod.POST })
	public ResultData<Object> editSysMenu(@RequestParam(required = true) Long sysMenuId,
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 100, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = false) String systemCode,
			@RequestParam(required = false) @Length(min = 0, max = 255, message = "参数[pageUrl]最大长度不能超过{max}") String pageUrl,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[pageUrlType]最大长度不能超过{max}") String pageUrlType,
			@RequestParam(required = false) Long parentId,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[site]最大长度不能超过{max}") String site,
			@RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) @Length(min = 0, max = 100, message = "参数[icon]最大长度不能超过{max}") String icon,
			@RequestParam(required = true) Integer menuType, @RequestParam(required = false) String description,
			@RequestParam(required = false) String buttonMark, @RequestParam(required = false) String field1,
			@RequestParam(required = false) String field2, @RequestParam(required = false) String field3) {
		SysMenu sysMenu = sysMenuService.selectById(sysMenuId);
		if (sysMenu == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysMenu = new SysMenu();
		if (sysMenuId != null) {
			sysMenu.setSysMenuId(sysMenuId);
		}
		if (StringUtils.isNotBlank(name)) {
			sysMenu.setName(name);
		}
		if (StringUtils.isNotBlank(systemCode)) {
			SysMenu sysMenuTmp = sysMenuService
					.selectOne(new EntityWrapper<SysMenu>(new SysMenu().setSystemCode(systemCode)));
			if (sysMenuTmp != null && sysMenuTmp.getSysMenuId() != sysMenuId) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【systemCode】的值：【" + systemCode + "】已经存在").build();
			}
			sysMenu.setSystemCode(systemCode);
		}
		if (StringUtils.isNotBlank(pageUrl)) {
			sysMenu.setPageUrl(pageUrl);
		}
		if (StringUtils.isNotBlank(pageUrlType)) {
			sysMenu.setPageUrlType(pageUrlType);
		}
		if (parentId != null) {
			sysMenu.setParentId(parentId);
		}
		if (StringUtils.isNotBlank(site)) {
			sysMenu.setSite(site);
		}
		if (sortNum != null) {
			sysMenu.setSortNum(sortNum);
		}
		if (StringUtils.isNotBlank(icon)) {
			sysMenu.setIcon(icon);
		}
		if (menuType != null) {
			sysMenu.setMenuType(menuType);
		}
		if (StringUtils.isNotBlank(description)) {
			sysMenu.setDescription(description);
		}
		if (StringUtils.isNotBlank(buttonMark)) {
			sysMenu.setButtonMark(buttonMark);
		}
		if (StringUtils.isNotBlank(field1)) {
			sysMenu.setField1(field1);
		}
		if (StringUtils.isNotBlank(field2)) {
			sysMenu.setField2(field2);
		}
		if (StringUtils.isNotBlank(field3)) {
			sysMenu.setField3(field3);
		}
		// sysMenu.setCreater(null);
		// sysMenu.setCreateTime(null);
		sysMenu.setUpdater(getCurrUserLoginId());
		sysMenu.setUpdateTime(new Date());
		sysMenuService.updateById(sysMenu);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【菜单信息】")
	@RequestMapping(value = "delSysMenu", method = { RequestMethod.POST })
	public ResultData<Object> delSysMenu(@RequestParam(required = true) Long sysMenuId) {
		SysMenu sysMenu = sysMenuService.selectById(sysMenuId);
		if (sysMenu == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		 sysMenuService.deleteById(sysMenuId);
		// 逻辑删除
		// sysMenu.setStatus(0);
		// sysMenuService.updateById(sysMenuId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【菜单信息】")
	@RequestMapping(value = "getSysMenu", method = { RequestMethod.POST })
	public ResultData<SysMenu> getSysMenu(@RequestParam(required = true) Long sysMenuId) {
		SysMenu sysMenu = sysMenuService.selectById(sysMenuId);
		if (sysMenu == null) {
			return new ResultData.Builder<SysMenu>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysMenu>(ResultCode.success).setData(sysMenu).build();
	}
	
	@ApiOperation(value = "获取【菜单信息】根据系统id")
	@RequestMapping(value = "getSysMenuBySystemCode", method = { RequestMethod.POST })
	public ResultData<SysMenu> getSysMenuBySystemCode(@RequestParam(required = true) String systemCode) {
		SysMenu sysMenu = sysMenuService.selectOne(new EntityWrapper<>(new SysMenu().setSystemCode(systemCode)));
		if (sysMenu == null) {
			return new ResultData.Builder<SysMenu>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysMenu>(ResultCode.success).setData(sysMenu).build();
	}
	@ApiOperation(value = "列表【菜单信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysMenuId", value = "主键", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "资源名称", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "menu_type等于0   的唯一 code    ", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrl", value = "资源路径", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "pageUrlType", value = "资源路径 跳转方式", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "parentId", value = "父级资源id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "site", value = "菜单所在位置（left  rigit top）", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "sortNum", value = "排序", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "icon", value = "资源图标", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "menuType", value = "资源类别 0系统 1菜单 2按钮 ", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "资源介绍", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "buttonMark", value = "按钮标签【查询query  列表查询queryList  增加save 编辑edit 删除 del】", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field1", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field2", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "field3", value = "", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "start_createTime", value = "开始-创建时间", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "end_createTime", value = "截至-创建时间", required = false), })
	@RequestMapping(value = "listSysMenu", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysMenu>> listSysMenu(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysMenuId, @RequestParam(required = false) String name,
			@RequestParam(required = false) String systemCode, @RequestParam(required = false) String pageUrl,
			@RequestParam(required = false) String pageUrlType, @RequestParam(required = false) Long parentId,
			@RequestParam(required = false) String site, @RequestParam(required = false) Integer sortNum,
			@RequestParam(required = false) String icon, @RequestParam(required = false) Integer menuType,
			@RequestParam(required = false) String description, @RequestParam(required = false) String buttonMark,
			@RequestParam(required = false) String field1, @RequestParam(required = false) String field2,
			@RequestParam(required = false) String field3, @RequestParam(required = false) String start_createTime,
			@RequestParam(required = false) String end_createTime) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键
		if (sysMenuId != null) {
			paramMap.put("sysMenuId", sysMenuId);
		}
		// 资源名称
		if (StringUtils.isNotBlank(name)) {
			paramMap.put("name", name);
		}
		// menu_type等于0 的唯一 code
		if (StringUtils.isNotBlank(systemCode)) {
			paramMap.put("systemCode", systemCode);
		}
		// 资源路径
		if (StringUtils.isNotBlank(pageUrl)) {
			paramMap.put("pageUrl", pageUrl);
		}
		// 资源路径 跳转方式
		if (StringUtils.isNotBlank(pageUrlType)) {
			paramMap.put("pageUrlType", pageUrlType);
		}
		// 父级资源id
		if (parentId != null) {
			paramMap.put("parentId", parentId);
		}
		// 菜单所在位置（left rigit top）
		if (StringUtils.isNotBlank(site)) {
			paramMap.put("site", site);
		}
		// 排序
		if (sortNum != null) {
			paramMap.put("sortNum", sortNum);
		}
		// 资源图标
		if (StringUtils.isNotBlank(icon)) {
			paramMap.put("icon", icon);
		}
		// 资源类别 0系统 1菜单 2按钮
		if (menuType != null) {
			paramMap.put("menuType", menuType);
		}
		// 资源介绍
		if (StringUtils.isNotBlank(description)) {
			paramMap.put("description", description);
		}
		// 按钮标签【查询query 列表查询queryList 增加save 编辑edit 删除 del】
		if (StringUtils.isNotBlank(buttonMark)) {
			paramMap.put("buttonMark", buttonMark);
		}
		//
		if (StringUtils.isNotBlank(field1)) {
			paramMap.put("field1", field1);
		}
		//
		if (StringUtils.isNotBlank(field2)) {
			paramMap.put("field2", field2);
		}
		//
		if (StringUtils.isNotBlank(field3)) {
			paramMap.put("field3", field3);
		}
		// 开始-创建时间
		if (StringUtils.isNotBlank(start_createTime)) {
			paramMap.put("start_createTime", start_createTime);
		}
		// 结束-创建时间
		if (StringUtils.isNotBlank(end_createTime)) {
			paramMap.put("end_createTime", end_createTime);
		}
		return sysMenuService.listSysMenu(paramMap);
	}
	
	@ApiOperation(value = "根据用户id查询 某个系统所拥有的全部菜单")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserId", value = "用户id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "systemCode", value = "系统code", required = false), })
	@RequestMapping(value = "listSysMenuByRoleAndUserId", method = { RequestMethod.POST })
	public ResultData<List<SysMenu>> listSysMenuByRoleAndUserId(@RequestParam(required = true) String sysUserId,
			@RequestParam(required = true) String systemCode,@RequestParam(required = false) String menuSite) {
		return sysMenuService.listSysMenuByRoleAndUserId(systemCode, sysUserId, menuSite);
	}
}
